// ve = vue element
let ve_user;
$(function () {
    let default_loginVO = { loginName: "", password: "" };
    ve_user = new Vue({
        el: "#user-data-box",
        data: {
            name: "",
            id: -1,
            loginVO: default_loginVO,
            loginErrorMessage: "",
            petOrders: [],
            commodityOrders: [],
            reservations: [],
            strays: [],
            comments: []
        },
        methods: {
            login() {
                if (ve_user.loginVO.loginName === "" || ve_user.loginVO.password === "") {
                    ve_user.loginErrorMessage = "账号和密码不能为空";
                    return;
                }

                ajax_post("/auth", ve_user.loginVO, function (response) {
                    if (response.code != null) {
                        switch (response.code) {
                            case "0004":
                                ve_user.loginErrorMessage = "账号或者密码不正确";
                                break;
                            default:
                                ve_user.loginErrorMessage = response.message;
                        }
                    }
                    else {
                        ve_user.loginErrorMessage = "";
                        ve_user.set_nav_bar_user_info(response);
                        ve_user.set_user_info();
                        ve_user.judge_if_to_show_login_box();
                    }
                    ve_user.loginVO = default_loginVO;
                }, "User login")
            },
            set_nav_bar_user_info(response) {
                ve_nav_bar.validation = response;
                // ve_nav_bar.validation.id = response.id;
                // ve_nav_bar.validation.name =response.name;
                // ve_nav_bar.validation.credit_number = response.creditNumber;
                // ve_nav_bar.validation.credit_password = response.creditPassword;
            },
            set_user_info() {
                ve_user.name = ve_nav_bar.validation.name;
                ve_user.id = ve_nav_bar.validation.id;
            },
            judge_if_to_show_login_box() {
                if (ve_nav_bar.validation.id > 0) {
                    show_login_box(false);
                    ve_user.set_user_info();
                    ve_user.get_orders_info();
                    ve_user.get_commodity_order_info();
                    ve_user.get_reservation_info();
                }
                else
                    show_login_box(true);
            },
            get_orders_info() {
                ve_user.get_pet_order_info();
            },
            get_pet_order_info() {
                ajax_get("/order/pet/" + ve_nav_bar.validation.id, {}, function (response) {
                    if (response.code == null) {
                        ve_user.petOrders = response;
                    }

                }, "Getting pet order info")
            },
            get_commodity_order_info() {
                ajax_get("/order/commodity/" + ve_nav_bar.validation.id, {}, function (response) {
                    if (response.code == null) {
                        ve_user.commodityOrders = response;
                    }else {
                        console.log(response.code)
                    }
                }, "Getting pet order info")
            },
            get_reservation_info() {
                ajax_get("/user/reservation/" + ve_nav_bar.validation.id, {}, function (response) {
                    console.log("GER INFO" + JSON.stringify(response))
                    if (response.code == null) {
                        ve_user.reservations = response;
                        ve_business_info.getReservationId();
                    }
                }, "Getting pet order info")
            },

            logout() {
                deleteCookie("token");
                ve_nav_bar.reset_info();
                ve_user.name = "";
                ve_user.id = -1;
                ve_user.petOrders = [];
                ve_user.commodities = [];
                ve_user.judge_if_to_show_login_box();
            },
            clear_register_message() {
                ve_user_register.errorMessage = "";
                ve_user_register.message = "";
            },
            render_my_pet_modal_box(id) {
                ve_my_pet_info.pet = ve_user.petOrders.find(order => order.id === id);
            },
            render_my_commodity_modal_box(id) {
                ve_my_commodity_info.commodity = ve_user.commodityOrders.find(order => order.id === id).commodity;
            },
            delete_reservation(id) {
                ajax_delete("/user/reservation/" + id, function (response) {
                    if (response) {
                        id.reserved = false;
                        ve_user.get_reservation_info();
                    }
                }, "Deleting reservation")
            }
        },
        computed: {
            is_logged_in() {
                return this.id > 0;
            },
            show_pet_orders() {
                return this.petOrders != null && this.petOrders.length > 0;
            },
            show_commodity_orders() {
                return this.commodityOrders != null && this.commodityOrders.length > 0;
            },
            show_reservations() {
                return this.reservations != null && this.reservations.length > 0;
            }
        }
    });

    let ve_user_register = new Vue({
        el: "#user-register",
        data: {
            name: "",
            loginName: "",
            password: "",
            re_password: "",
            errorMessage: "",
            phoneNumber: "",
            creditNumber: "",
            creditPassword: "",
            message: ""
        },
        methods: {
            checkAndRegister() {
                ve_user_register.message = "";
                ve_user_register.errorMessage = "";

                if (ve_user_register.password != ve_user_register.re_password) {
                    ve_user_register.errorMessage = "两次输入不一致！";
                    return;
                }
                if (ve_user_register.loginName.length < 6 || ve_user_register.password.length < 6) {
                    ve_user_register.errorMessage = "请输入大于等于 6 位的账号和密码！";
                    return;
                }
                if (ve_user_register.name.length < 2) {
                    ve_user_register.errorMessage = "请输入大于等于 2 位的用户名！";
                    return;
                }

                if (ve_user_register.phoneNumber.length != 11 && !isNaN(ve_user_register.phoneNumber)) {
                    ve_user_register.errorMessage = "请输入正确格式的号码（11位数字）";
                    return;
                }

                ajax_get("/user/check", {
                    loginName: ve_user_register.loginName
                }, function (response) {
                    if (response ){
                        ve_user_register.register();
                    } else {
                        ve_user_register.errorMessage = "账号已存在，请重新输入！";
                    }
                }, "Check loginName")
            },
            register() {
                ajax_post("/user", {
                    name: ve_user_register.name,
                    phoneNumber: ve_user_register.phoneNumber,
                    loginName: ve_user_register.loginName,
                    password: ve_user_register.password,
                    creditNumber: ve_user_register.creditNumber,
                    creditPassword: ve_user_register.creditPassword
                }, function (response) {
                    if (response.code != null) {
                        switch (response.code) {
                            default: ve_user_register.errorMessage = response.message;
                        }
                    } else {
                        ve_user_register.message = ve_user_register.name + "，您已注册成功，可在个人中心登录";
                        ve_user_register.loginName = "";
                        ve_user_register.name = "";
                        ve_user_register.password = "";
                        ve_user_register.re_password = "";
                        ve_user_register.errorMessage = "";
                        ve_user_register.phoneNumber = "";
                        setTimeout(function () {
                            $("#user-register").modal('hide');
                        }, 1000);
                    }
                })
            }
        },
        computed: {
            fields_empty() {
                return this.name === "" || this.loginName === "" || this.password === "" || this.re_password === "" || this.phoneNumber === "";
            }
        }
    });

    new Vue({
        el: "#user-logout",
        methods: {
            logout() {
                ve_user.logout();
            }
        }
    });

    function show_login_box(show) {
        if (show) {
            $("#user-info-box").hide();
            $("#user-login-box").show();
        } else {
            $("#user-info-box").show();
            $("#user-login-box").hide();
        }
    }

    let ve_my_pet_info = new Vue({
        el: "#my-pet-info",
        data: {
            pet: {
                petName: "",
                petImage: "",
                description: "",
                birthDate: "",
                speciesName: "",
                speciesDescription: ""
            }
        }
    });

    let ve_my_commodity_info = new Vue({
        el: "#my-commodity-info",
        data: {
            commodity: {
                name: "",
                image: "",
                description: "",
                type: "",
                producer: {
                    name: "",
                    location: "",
                    contactNumber: ""
                }
            }
        }
    });
});